package com.ipad.net.presenter;

import com.ipad.net.ApiCallback;
import com.ipad.net.ApiClient;
import com.ipad.net.BasePresenter;
import com.ipad.net.apistore.UserApi;
import com.ipad.net.entity.LoginEntity;
import com.ipad.net.entity.UserInfo;
import com.ipad.net.entity.WarehouseList;
import com.ipad.net.view.IUserView;
import com.ipad.utils.LogUtils;
import com.ipad.utils.OkHttpUtils;
import com.ipad.utils.ToasUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2018/12/4.
 */

public class UserPresent extends BasePresenter<IUserView> {
    UserApi userApi;

    public UserPresent() {
        userApi = ApiClient.getInstance().getjRetorfit().create(UserApi.class);
    }

    public void login(String userName, String password) {
        t.loading("登录中..");
        addSubscribe(userApi.login(userName, password), new ApiCallback<LoginEntity>() {
            @Override
            public void onSuccess(LoginEntity o) {
                t.login(o);
            }

            @Override
            public void onFailuer(final String msg) {
                post(new Runnable() {
                    @Override
                    public void run() {
                        UserPresent.super.t.error(msg);
                    }
                });
            }

            @Override
            public void onFinish() {
                post(new Runnable() {
                    @Override
                    public void run() {
                        t.stopLoading();
                    }
                });
            }
        });

    }

    public void getUserInfo(String token) {
//        t.loading("登录中..");
        addSubscribe(userApi.getUserInfo(token), new ApiCallback<UserInfo>() {
            @Override
            public void onSuccess(UserInfo o) {

                t.getUserInfo(o);
            }

            @Override
            public void onFailuer(final String msg) {
//                post(new Runnable() {
//                    @Override
//                    public void run() {
//                        t.error(msg);
//                    }
//                });
                t.getUserError();
            }

            @Override
            public void onFinish() {
                post(new Runnable() {
                    @Override
                    public void run() {
                        t.stopLoading();
                    }
                });
            }
        });
    }

    public void getWarehouseList(String type,String token, String pageNumber, String pageSize, String name,boolean isloading) {
        if (isloading){
            t.loading("");
        }
        addSubscribe(userApi.getUserWarehouseList(type,token, pageNumber, pageSize, name), new ApiCallback<WarehouseList>() {
            @Override
            public void onSuccess(WarehouseList o) {
                t.getWarehouseList(o);
            }

            @Override
            public void onFailuer(String msg) {
                t.error(msg);
            }

            @Override
            public void onFinish() {
                t.stopLoading();
            }
        });
    }
}
